from game import zz_game
from zz import zz_log, zz_img, zz_code
import uiautomator2 as u2

log = zz_log.of()


class Game(zz_game.ZZ_Game):
    def __init__(self, d, play_boss=True, has_prize=True):
        super().__init__(d, has_prize)
        self.play_boss = play_boss  # 是否挑战boss

    def get_name(self):
        return "灵剑仙师"

    def game_alert(self):
        """
        广告
        :return:
        """
        self.alert_close_by_file("./game/img/灵剑仙师")

    def play_alert(self):
        if not self.img_exit("./game/img/灵剑仙师/play_alert.png", 3):
            self.log_msg("不存在弹窗")
            return False

        if self.img_exit("./game/img/灵剑仙师/play_state.png", 2):
            self.log_msg("战力下降，点击出售")
            self.img_click("./game/img/灵剑仙师/play_buy.png")
        else:
            self.log_msg("战力上升，点击收服")
            self.img_click("./game/img/灵剑仙师/play_get.png")

        if self.img_exit("./game/img/灵剑仙师/play_confirm.png", 1):
            self.log_msg("存在确认页面, 点击确认")
            self.img_click("./game/img/灵剑仙师/play_confirm_ok.png")
        return True

    def handle_main(self):
        self.log_msg("等待进入游戏页面")
        self.img_exit("./game/img/灵剑仙师/boss01.png", 100)
        self.log_msg("已进入游戏页面")

        self.game_alert()

        if self.not_prize_handle():
            return False

        if self.play_boss:
            self.img_click_if_exists("点击挑战副本", './game/img/灵剑仙师/boss01.png', './game/img/灵剑仙师/boss02.png')
            self.log_msg('点击boss')
            self.img_click("./game/img/灵剑仙师/boss02.png")
            if self.img_click("./game/img/灵剑仙师/boss03.png"):
                self.log_msg('点击取消弹框')
                self.click_bottom()
            self.log_msg("挑战副本-完成")
            self.log_msg("退出副本")
            self.img_click("./game/img/灵剑仙师/alert_0_00.png")
            self.log_msg("退出副本")
            self.img_click("./game/img/灵剑仙师/alert_0_00.png")

        if self.not_prize_handle():
            return False

        self.log_msg("开始异火修炼")
        while not self.check_done():
            for i in range(5):
                self.log_msg(f"{i}-点击修炼异火")
                self.img_click("./game/img/灵剑仙师/play.png", 5)
                self.play_alert()
            self.game_alert()

        return True
